/*************************************************************************************
**																					**
**	microapi.h			Renesas RL78 R5F100EEANA         							**
** 																					**
**************************************************************************************
**																					**
** Written By:	Steve Montgomery													**
**				Digital Six Laboratories LLC										**
** (c)2012,2013,2014 Digital Six Labs, All rights reserved							**
**																					**
**************************************************************************************/
//
// Revision History
//
// Revision		Date	Revisor		Description
// ===================================================================================
// ===================================================================================
/*! \addtogroup Microcontroller Microcontroller API
 *
 * \ingroup PortedAPIs
 * @{
 * \details This is the microcontroller API for the RL78 micro.
 *
 */
#ifndef MICROAPI_DEFS_H
#define MICROAPI_DEFS_H
#include "TypeDefinitions.h"
#include "hardware_defs.h"                    // requires compiler_defs.h
#include "iodefine.h"
#include "iodefine_ext.h"
#include "interrupt_handlers.h"
#include "pfdl.h"
#include "pfdl_types.h"

/*! \details Defines available baud rates for UART
 */
enum
{
	k2400,
	k4800,
	k9600,
	k19200,
	k38400,
	k57600,
	k76800,
	k115200
};

/*! \details Defines all of the different interrupt types that are all available.
 */
typedef enum
{ /*!This is the Radio interrupt */
	NIRQ=0
} InterruptTypes;
/*! \details Defines the configuration structure for the A/D converter
 *
 */
typedef struct
{
	U8 TriggerMode;
	U8 ConversionMode;
	U8 ChannelSelectionMode;
	U8 ConversionRate;
	U8 HardwareTrigger;
	U8 ReferenceSource;
	U8 LimitCheckMode;
	U8 SnoozeMode;
	U8 UpperLimit;
	U8 LowerLimit;
	U8 PowerMode;
} tAnalogConfiguration;


// ADM0
#define kEnableAnalogConversion 0x80
#define kDisableAnalogConverstion 0x00
#define kSelectAnalogChannel 0x00
#define kScanAnalogChannel 0x40
#define kEnableComparatorOperation 0x01
#define kDisableComparatorOperation 0x00
#define kConversionClockDiv64 0x00
#define kConversionClockDiv32 0x08
#define kConversionClockDiv16 0x10
#define kConversionClockDiv8 0x18
#define kConversionClockDiv6 0x20
#define kConversionClockDiv5 0x28
#define kConversionClockDiv4 0x30
#define kConversionClockDiv2 0x38
#define kConversionModeNormal1 0x00
#define kConversionModeNormal2 0x02
#define kConversionModeLowVoltage1 0x04
#define kConversionModeLowVoltage2 0x06
// ADM1
#define kSoftwareTriggerMode 0x00
#define kHardwareTriggerNoWaitMode 0x80
#define kHardwareTriggerWaitMode 0xc0
#define kSequentialConversionMode 0x00
#define kOneShotConversionMode 0x20
#define kHardwareTriggerTimer1 0x00
#define kHardwareTriggerRTC 0x02
#define kHardwareTriggerIntervalTimer 0x03
// ADM2
#define kAnalogPositiveReferenceVdd 0x00
#define kAnalogPositiveReferenceP20 0x40
#define kAnalogPositiveReferenceInternal 0x80
#define kAnalogNegativeReferenceVss 0x00
#define kAnalogNegativeReferenceP21 0x20
#define kAnalogOutsideWindowInterrupt 0x00
#define kAnalogInsideWindowInterrupt 0x08
#define kAnalogNoSnooze 0x00
#define kAnalogSnooze 0x04
#define kAnalog10BitResolution 0x00
#define kAnalog8BitResolution 0x01


// ******************************************************************************************************************************
// *** Public API ***

/*! \details This function initializes the API.  When done, the micro is in its post reset default state.
 * \return Reset reason.  Microcontroller dependent.  Refer to particular implementation.
 */
U8 InitializeMicroAPI(void);

void InitializeTimers(void);
/*! \details This function reads one byte from the SPI from a given location
 * \return Byte that was read.
 */
U8 ReadSPI(U8 reg /*! Register (location) to read */) reentrant;
/*! \details Write a single byte to a specific location on the SPI port
 * \return 0 if successful, >=1 if not.
 */
U8 WriteCharSPI(U8 reg /*! Register */, U8 data /*! Byte to write */) reentrant;

/*! \details Read a character from the receive buffer for UART0
 *  \return Character from buffer if available, null if not
 */
U8 ReadCharUART0(void);
/*! \details Write a character to the transmit buffer for UART0
 *  \return None
 */
void WriteCharUART0(U8 charToWrite /*! Character to write to buffer */);
/*! \details Get the number of characters in UART0 receive buffer
 *  \return Count
 */
U8 BufferCountUART0(void);
/*! \details Read a character from the receive buffer for UART1
 *  \return Character from buffer if available, null if not
 */
U8 ReadCharUART1(void);
/*! \details Write a character to the transmit buffer for UART0
 *  \return None
 */
void WriteCharUART1(U8 charToWrite/*! Character to write to buffer */);
/*! \details Get the number of characters in UART1 receive buffer
 *  \return Count
 */
U8 BufferCountUART1(void);
/*! \details Reads one char(byte) from a specified address on the IIC port
 *  \return Character read
 */
U8 ReadCharIIC(U8 address/*! Address to read from */);
/*! \details Reads specified number of characters from a specified starting address
 *  \return 0 if successful, >=1 if not.l
 */
U8 ReadMultipleIIC(U8 count/*! Number of bytes to read */,
		U8 address/*! Address  to start at*/,
		U8 *buffer/*! Buffer to hold received bytes */);
/*! \details Write a char(byte) to a specified address on the IIC port
 *  \return 0 if successful, >=1 if not.
 */
U8 WriteCharIIC(U8 address/*! Address to write to*/,
		U8 byteToWrite /*! Byte to write to address */);
/*! \details Write specified number of characters to specified starting address
 *  \return 0 if successful, >=1 if not.
 */
U8 WriteMultipleIIC(U8 count /*! Number of bytes to write*/,
		U8 address /*! Starting address */,
		U8 *buffer /*! Buffer to write from */);

/*! \details Set the main clock to use the sub oscillator
 *  \return none
 */
void SetMainClockToSubOscillator(void);
/*! \details Turns the high speed oscillator off
 *  \return none
 */
void InternalHSOscillatorOff(void);
/*! \details Turns the high speed oscillator on
 *  \return none
 */
void InternalHSOscillatorOn(void);
/*! \details Turn the sub oscillator off
 *  \return none
 */
void SubOscillatorOff(void);
/*! \details Turn the sub oscillator on
 *  \return none
 */
void SubOscillatorOn(void);
/*! \details Set the sub-oscillator mode
 *  \return none
 */
void SetSubOscillatorMode(U8 mode /*! 0=low power, 1=normal, 2=Ultra low power*/);
/*! \details Turn on real time clock
 *  \return none
 */
void RealTimeClockOn(void);
/*! \details Turn off real time clock
 *  \return none
 */
void RealTimeClockOff(void);
/*! \details Enable 1Hz output
 *  \return none
 */
void EnableRTC1HZ(void);
/*! \details Disable 1Hz output
 *  \return none
 */
void DisableRTC1HZ(void);
/*! \details Sets 12 or 24 hour mode
 *  \return none
 */
void SetRTCHourMode(U8 mode /*! 0=12 hour, 1=24 hour*/);
/*! \details Sets the constant period interrupt
 *  \return none
 */
void SetCPI(U8 mode /*! 0=Off, 1= .5sec, 2=1 sec, 3=1 minute, 4=1 hour, 5=1 day, 6=1 month */);
/*! \details Enables RTC alarm
 *  \return none
 */
void EnableRTCAlarm(U8 enableInterrupt /*! 0=disable, 1=enable */);
/*! \details Disables RTC alarm
 *  \return none
 */
void DisableRTCAlarm();
/*! \details Hits the watchdog timer
 * \return none
 */
void HitWDT(void);
/*! \details Turn analog converter on
 *  \return none
 */
void AnanlogConversionOn(void);
/*! \details Turn analog converter off
 *  \return none
 */
void AnalogConversionOff(void);
/*! \details Start converting analog
 *  \return none
 */
void AnalogConversionStart(void);
/*! \details Stop converting analog
 *  \return none
 */
void AnalogConversionStop(void);
/*! \details Configure the analog converter
 *  \return none
 */
void AnalogConverterConfigure(tAnalogConfiguration config /*! Configuration data */);
/*! \details Get result of last conversion (10 bits)
 *  \return 10 bit result in 16 bit format
 */
U16 AnalogGet10BitResult();
/*! \details Get result of last conversion (8 bits)
 *  \return 10 bit result in 8 bit format
 */
U8 AnalogGet8BitResult();
/*! \details Set the input channel
 *  \return none
 */
void AnalogSetInputChannel(U8 channel /*! Input channel */);
/*! \details Starts the interval timer.  Handle1MsInterval() will be called every millsecond
 *  \return none
 */
void StartIntervalTimer();
/*! \details Stops the interval timer.  Handle1Msnterval() will not be called after this returns.
 * \return none
 */
void StopIntervalTimer();

void EnableSPI();

void SetIOForReceive();
void SetIOForTransmit();

void WritePersistentValue(U16 address, U8 *value, U8 count);
U8 ReadPersistentValue(U16 address);
void ErasePersistentArea();
void SetUART1BaudRate(U8 baudRate);
U8 GetUART1BaudRate();
void EnableIntP0();
void DisableIntP0();
void UpdateRTC(UU32 timeStamp);

#define INITIALIZEDVALUE 0x55
// Disable interrupts
#define DisableInterrupts DI();
// Enable interrupts
#define EnableInterrupts EI();

// Get the interrupt pin status for the radio
#define NIRQStatus IRQ
// Gets the status of the SDN pin
#define SDNPin SDN

#define kAnalogTriggerSoftware 0x00
#define kAnalogTriggerHardwareNoWait 0x80
#define kAnalogTriggerHardware 0xc0

#define kAnalogConversionModeSequential 0x00
#define kAnalogConversionModeOneShot 0x20

#define kAnalogHardwareTriggerINTTM01 0x00
#define kAnalogHardwareTriggerINTRTC 0x02
#define kAnalogHardwareTriggerINTIT 0x03

#define kAnalogChannelSelectMode 0x00
#define kAnalogChannelScanMode 0x40

#define kAnalogPositiveReferenceVDD 0x00
#define kAnalogPositiveReferenceP20 0x40
#define kAnalogPositiveReferenceInternal 0x80
#define kAnalogNegativeReferenceVSS 0x00
#define kAnalogNegativeReferenceP21 0x20

#define kAnalogLimitCheckInband 0x00
#define kAnalogLimitCheckOutsideBand 0x08

#define kAnalogSnoozeEnabled 0x00
#define kAnalogSnoozeDisabled 0x04

#define kAnalogResolution10Bits 0x00
#define kAnalogResolution8Bits 0x01
void ResetRadio(void);



// This must be defined in the radioapi.  It will be called by the microapi when an interrupt happens that needs to be 
// handled by the radioapi
extern void HandleInterrupt(U8 intType);
// This must be defined in radioapi.  It will be called once every millisecond by the Timer3 interrupt
extern void Handle1MsInterrupt();
// This must be defined in radioapi.  It will be called once every second by the RTC interrupt
extern void Handle1SecInterrupt();

// ******************************************************************************************************************************
#define KEY_WAITTIME                    (8U)      /* Wait 250 */

#define pinRadioNSS P12_bit.no0
#define pinRadioDIO0 P13_bit.no7
#define pinRadioDIO1 P5_bit.no0
#define pinRadioDIO2 P5_bit.no1
#define pinRadioDIO3 P3_bit.no0
#define pinRadioDIO4 P3_bit.no1
#define pinRadioDIO5 P1_bit.no6
#define pinRadioReset P2_bit.no6
#define SLEEP asm("stop")
#define I2C_DAT P6_bit.no1
#define I2C_CLOCK P6_bit.no0

// NOTE: Reset is not connected to the micrcontroller on the rfBrick
//#define RadioResetPin P2_bit.no6
#define HIGH 1
#define LOW 0
// these are globally defined to provide non-architecture specific code the ability to globally enable and disable interrupts
//#define EnableInterrupts EI()
//#define DisableInterrupts DI()

#endif

/*!
 * @}
 */
